package gui.小练习.打字游戏;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 * @Package gui.小练习.打字游戏
 * @Author shimmer
 * @Date 2020/8/7 15:05
 * @TODO
 */
public class GameKeyListener extends KeyAdapter {
    GameJPanel gameJPanel=null;
    ArrayList<EnemyInfo> enemyList=null;

    public GameKeyListener(GameJPanel gamePanel, ArrayList<EnemyInfo> enemyList) {
        this.gameJPanel = gamePanel;
        this.enemyList=enemyList;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode()==KeyEvent.VK_SPACE){// 空格
            if (gameJPanel.isFail) {
                //重新开始
                gameJPanel.isFail = false;
                gameJPanel.init();
            } else{
                //因为需要反复变换所以不能写死了
                gameJPanel.isStart = !gameJPanel.isStart;
            }
            gameJPanel.repaint();//重绘，否则没有反应
        }else{
            for (EnemyInfo enemy : enemyList) {
                if (e.getKeyCode()==enemy.aim){
                    //添加子弹
                    BulletInfo bullet = new BulletInfo();
                    bullet.setBulletX(gameJPanel.playerX+40);
                    bullet.setBulletY(gameJPanel.playerY-20);
                    bullet.setEnemy(enemy);
                    gameJPanel.bulletList.add(bullet);
                    //发射子弹
                    gameJPanel.isLaunch=true;
                    break;// 一次只发射一颗
                }
            }
        }
    }
    
}
